################################################################################
# WeBWorK Online Homework Delivery System
# Copyright &copy; 2000-2018 The WeBWorK Project, http://openwebwork.sf.net/
# $CVSHeader$
# 
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
# Artistic License for more details.
################################################################################

=head1 NAME

PGmiscevaluators.pl - Some miscellaneous answer macros.

=head1 SYNOPSIS

=head1 DESCRIPTION

Currently contains answer evaluators for radio buttons and checkboxes.

=head2 MathObjects and answer evaluators

The MathObjects system provides a parserRadioButtons.pl file that manages
display and checking of radio-button based answers. It is recommended that you
use this method directly if possible.

=cut

BEGIN { be_strict() }
sub _PGmiscevaluators_init {}

=head1 checkbox_cmp

	ANS(checkbox_cmp($correctAnswer))

$correctAnswer is a string containing the names of the correct boxes, e.g.
"ACD". Note that this means that individual checkbox names can only be one
character. Internally, this is largely the same as unordered_cs_str_cmp().

=cut

# added 6/14/2000 by David Etlinger
# because of the conversion of the answer
# string to an array, I thought it better not
# to force STR_CMP() to work with this

#added 2/26/2003 by Mike Gage
# handled the case where multiple answers are passed as an array reference
# rather than as a \0 delimited string.
sub checkbox_cmp {
	my	$correctAnswer = shift @_;
	my %options = @_;
	assign_option_aliases( \%options,
     );
    set_default_options(	\%options,
    			'debug'					=>	0,
	       		'type'                  =>  'checkbox_cmp',
    );
	my $answer_evaluator = new AnswerEvaluator(
		correct_ans      => $correctAnswer,
		type             => $options{type},
	);
	# pass along debug requests
	$answer_evaluator->{debug} = $options{debug};
	
	# join student answer array into a single string if necessary
	$answer_evaluator->install_pre_filter(sub {
		my $rh_ans = shift;
		$rh_ans->{_filter_name} = 'convert student_ans to string';
		$rh_ans->{student_ans} = join("", @{$rh_ans->{student_ans}}) 
		         if ref($rh_ans->{student_ans}) =~/ARRAY/i;
		$rh_ans;
	});
	# ignore order of check boxes
	$answer_evaluator->install_pre_filter(\&ignore_order);
	# compare as strings
	$answer_evaluator->install_evaluator(sub {
		my $rh_ans     = shift;
		$rh_ans->{_filter_name} = 'compare strings generated by checked boxes';
		$rh_ans->{score} = ($rh_ans->{student_ans} eq $rh_ans->{correct_ans}) ? 1 : 0;
		$rh_ans;
	});
	# fix up preview displays
	$answer_evaluator->install_post_filter( sub {
		my $rh_ans      = shift;
		$rh_ans->{_filter_name} = 'adjust preview strings';
		$rh_ans->{type} = $options{type};
		$rh_ans->{preview_text_string}	=	'\\text{'.$rh_ans->{student_ans}.'}',
		$rh_ans->{preview_latex_string}	=	'\\text{'.$rh_ans->{student_ans}.'}',
		$rh_ans;
	
	
	});
	
	return $answer_evaluator;
}

=head1 radio_cmp

	ANS(radio_cmp($correctAnswer))

$correctAnswer	is a string containing the name of the correct radio button,
e.g. "Choice1". This is case sensitive and whitespace sensitive, so the correct
answer must match the name of the radio button exactly.

=cut

#added 6/28/2000 by David Etlinger
#exactly the same as strict_str_cmp,
#but more intuitive to the user

# check that answer is really a string and not an array
# also use ordinary string compare
sub radio_cmp {
	#strict_str_cmp( @_ );
	my $response = shift;  # there should be only one item.
	warn "Multiple choices -- this should not happen with radio buttons. Have
	you used checkboxes perhaps?" if ref($response); #triggered if an ARRAY is passed
	str_cmp($response);
}

=head1 SEE ALSO

L<PGanswermacros.pl>, L<MathObjects>.

=cut

1;
